package com.ruoyi.DataManage.service.impl;

import java.util.List;

import com.ruoyi.DataManage.domain.Province;
import com.ruoyi.DataManage.domain.Subject_tree;
import com.ruoyi.DataManage.mapper.CategoryMapper;
import com.ruoyi.DataManage.mapper.ProvinceMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.DataManage.domain.Category;
import com.ruoyi.DataManage.mapper.UniversityMapper;
import com.ruoyi.DataManage.domain.University;
import com.ruoyi.DataManage.service.IUniversityService;

/**
 * 高校名录管理Service业务层处理
 *
 * @author CehnFneg
 * @date 2024-04-11
 */
@Service
public class UniversityServiceImpl implements IUniversityService {
    @Autowired
    private UniversityMapper universityMapper;

    @Autowired
    private ProvinceMapper provinceMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询高校名录管理
     *
     * @param id 高校名录管理主键
     * @return 高校名录管理
     */
    @Override
    public University selectUniversityById(Long id) {
        return universityMapper.selectUniversityById(id);
    }

    /**
     * 查询高校名录管理列表
     *
     * @param university 高校名录管理
     * @return 高校名录管理
     */
    @Override
    public List<University> selectUniversityList(University university) {
        return universityMapper.selectUniversityList(university);
    }

    /**
     * 新增高校名录管理
     *
     * @param university 高校名录管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUniversity(University university) {
        university.setCreateTime(DateUtils.getNowDate());
        int rows = universityMapper.insertUniversity(university);
        List<Category> categoryList = university.getCategoryList();
        if(!categoryList.isEmpty()){
            insertCategory(university);
        }
        return rows;
    }

    /**
     * 修改高校名录管理
     *
     * @param university 高校名录管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateUniversity(University university) {
        university.setUpdateTime(DateUtils.getNowDate());
        Long id = provinceMapper.getProvinceIdByName(university.getProvinceName());
        university.setProvinceId(id);
        insertCategory(university);
        return universityMapper.updateUniversity(university);
    }

    /**
     * 批量删除高校名录管理
     *
     * @param ids 需要删除的高校名录管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUniversityByIds(Long[] ids) {
        universityMapper.deleteCategoryByUniversityIds(ids);
        return universityMapper.deleteUniversityByIds(ids);
    }

    /**
     * 删除高校名录管理信息
     *
     * @param id 高校名录管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUniversityById(Long id) {
        universityMapper.deleteCategoryByUniversityId(id);
        return universityMapper.deleteUniversityById(id);
    }

    @Override
    public List<Province> getProvinceList() {
        return universityMapper.getProvinceList();
    }

    @Override
    public List<University> getUniversityList() {
        return universityMapper.getUniversityList();
    }

    @Override
    public List<University> getUniversityListByProvinceId(Long provinceId) {
        if(provinceId == -1) return universityMapper.getAllUniversityList();
        return universityMapper.getUniversityListByProvinceId(provinceId);
    }

    /**
     * 新增门类管理信息
     *
     * @param university 高校名录管理对象
     */
    public void insertCategory(University university) {
        List<Category> categoryList = university.getCategoryList();
        Long id = university.getId();
        if (StringUtils.isNotNull(categoryList)) {
            categoryMapper.deleteAll(id);
            for (Category category : categoryList) {
                category.setUniversityId(id);
            }
            universityMapper.batchCategory(categoryList);
        }
    }
}
